<?php
/**
 * Created by PhpStorm.
 * User: yzapple
 * Date: 2018/1/8
 * Time: 下午4:48
 * 就诊人
 */

namespace app\index\controller;
use think\Db;

class Userfamily extends Basecontroller
{
    protected $noNeedLogin = [
        'check_case',
        'check_list',
        'electronic_report',
        'electronic_film',
        'check_detail'
    ];
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Userfamily');

    }

    /**
     * 就诊人列表
     */
    public function index()
    {
        $this->user->id = 77;
        $where = ['is_deleted' => self::DELETE_NO, 'userid' => $this->user->id];

        $param = $this->request->param();
        //获取分页参数
        $page = isset($param['page'])?$param['page'] : 1;
        $userfamilylist = $this->getUserfamilylist($where, $page);
        $new_userfamily_list = [];
        //整理数据
        foreach ($userfamilylist as $key => $value) {

            // $userfamily_info = $this->getUserfamily_Bydocnum($value['document_num']);
            $userfamily_info = $this->getPatient_Bydocnum($value['document_num']);
            $option['patient_id'] = $userfamily_info['id'];
            $value['record_count'] = $this->getUserfamilycase_count($option);
            if ($value['sex'] == 1) {
                $value['header'] = APP_DOMAIN.'/public/static/index/imgs/head_nan.jpg';
            } else {
                $value['header'] =  APP_DOMAIN.'/public/static/index/imgs/head_nv.jpg';
            }
            $value['detail_url'] = url('userfamily/check_list', array('pid'=>$value['id']));
            $new_userfamily_list[] = $value;
        }
//        $this->assign('list', $new_userfamily_list);

//        if ($new_userfamily_list) {
//            foreach ($new_userfamily_list as &$v) {
//                if ($v['sex'] == 1) {
//                    $v['header'] = '../../../../public/static/index/imgs/head_nan.jpg';
//                } else {
//                    $v['header'] = '../../../../public/static/index/imgs/head_nv.jpg';
//                }
//                $v['detail_url'] = url('userfamily/check_list', array('pid'=>$v['id']));
//            }
//        }
        //返回数据
        if ($this->request->isPost()){
            $res['msg'] = "成功";
            if ($new_userfamily_list){
                $res['data'] = $new_userfamily_list;
            } else {
                $res['msg'] = '暂无数据';
                $res['data'] = [];
            }
            return json($res);
        }
        return $this->fetch();
    }

    /**
     * 添加就诊人
     */
    public function add_userfamily()
    {
        if ($this->request->isPost()) {

            $params['name'] = $this->request->param('name');
            $params['sex'] = $this->request->param('sex');
            $params['document_type'] = $this->request->param('document_type');
            $params['document_num'] = $this->request->param('document_num');
            $params['birth'] = $this->request->param('birth');
            $params['doctor_id'] = $this->user->id;
            $params['userid'] = $this->user->id;

            $result = $this->validate($params, 'app\index\validate\Userfamily');
            if (true !== $result) {
                $this->error($result);
            }
            if(DB::name('user_family')->where("document_num='{$params['document_num']}' and name='{$params['name']}' and is_deleted=1 and userid={$this->user->id}")->find()){
                $this->error("已经在列表中,请不要重复添加");
            }

            $id_num_check = is_idcard( $this->request->param('document_num'));
            if(!$id_num_check){
                $this->error("身份证号不正确");
            }
            /*  $resullt = DB::name('user_family')
                          ->join("patient","patient.document_num=user_family.document_num")
                          ->where("user_family.document_num={$params['document_num']} and user_family.name={$params['name']}")->find();
              if(!$resullt){
                  $this->error("当前没有该患者的记录,请确认之后在添加");
              }*/

            if ($this->model->create($params)) {

                $this->success("添加成功", url('userfamily/index'));

            } else {
                $this->error("添加失败");
            }
        }


        return $this->fetch();

    }

    /**
     * 就诊人病例记录-废弃
     */
    public function userfamily_recordlist()
    {
        $pid = $this->request->param('pid');
        $userfamily = $this->getUserfamilyById($pid);
        $this->assign('userfamily', $userfamily);

        return $this->fetch();

    }


    /**
     * 检查记录
     */
    public function check_list()
    {
        $param = $this->request->param();

        $pid = $param['pid'];
        $expire = isset($param['expire']) ? $param['expire'] : '';
        $this->assign('expire', $expire);
        $isCheck = isset($param['isCheck']) ? $param['isCheck'] : '';
        $this->assign('isCheck', $isCheck);
        //分页加载
        $page = isset($param['page'])?$param['page'] : 1;

        $userfamily = $this->getUserfamilyById($pid);

        $patient_info = $this->getPatientID_Bydocnum($userfamily['document_num']);
        $option['patient_id'] = $patient_info['id'];

        $case_list = $this->getCase_list($option, $page);

//        $cc = array(array('3' => '4'), array('3' => '5'), array('2' => '5'));
//
//        $ddd = json_encode($cc);
//        $aaa = json_decode($ddd, true);

        $new_case_list = [];
        foreach ($case_list as $key => $value) {

            $patient_case_type = $this->getPatient_case_type($value['patient_case_type']);
            $value['patient_case_type_name'] = $patient_case_type['name'];
            // $part_lat_name = $this->getPart_lat($value['patient_part_lat'], 1);
            $part_lat_name = json_decode($value['patient_part_lat'], true);
            $value['patient_part_lat_name'] = $part_lat_name;
            $new_case_list[] = $value;
        }

        $this->assign('userfamily', $userfamily);

        if ($this->request->isAjax()){
            foreach ($new_case_list as $k => $v) {
                $v['jump_url'] = url('userfamily/check_detail', array('cid' =>$v['id']));
                $v['check_time'] = date('Y-m-d', strtotime($v['check_time']));
            }
            $res['msg'] = '成功';
            $res['data'] = $new_case_list;
            return json($res);
        }else{
            $this->assign('case_list', $new_case_list);
        }
        $this->assign('pid', $pid);
        return $this->fetch();


    }

    /**
     *
     *删除患者
     */
    public function del_userfamily()
    {
        $pid = $this->request->param('pid');
        $this->delete_userfamily($pid);
        $this->success("删除成功", url('userfamily/index'));
    }

    /**
     * 检查详情
     */
    public function check_detail()
    {
        $param = $this->request->param();
        $cid = $this->request->param('cid');
        $pid = $this->request->param('pid');
        $expire = isset($param['expire']) ? $param['expire'] : '';
        //print_r($expire);
        $this->assign('expire', $expire);

        $isCheck = isset($param['isCheck']) ? $param['isCheck'] : '';
        $this->assign('isCheck', $isCheck);
        //获取病人信息以及病例
        $this->getPatient_info_case($cid);
        $this->assign('pid', $pid);
        return $this->fetch();

    }

    /**
     * 电子报告
     */
    public function electronic_report()
    {
        //不知道几张报告 样式可能要改
        $cid = $this->request->param('cid');
        $this->getPatient_report($cid);
        return $this->fetch();

    }

    /**
     * 电子胶片
     */
    public function electronic_film()
    {
        $cid = $this->request->param('cid');
        $electronic_film = "";
        $patientcase_info = model('Patientcase')->field('patient_case.electronic_film')
            ->where('patient_case.id', $cid)
            ->find();
        $electronic_film = json_decode($patientcase_info['electronic_film'],true);


        // print_r($electronic_film_string);
        $this->assign("electronic_film",$electronic_film);
        return $this->fetch();

    }

    /**
     * 影像问诊
     */
    public function img_interrogation()
    {
        return $this->fetch();

    }
    public function yingxiangchuli()
    {

        $cid = $this->request->param('cid');
        //$cid = 29;
        $patientcase_info = model('Patientcase')->field('patient_case.electronic_film')
            ->where('patient_case.id', $cid)
            ->find();
        $electronic_film = json_decode($patientcase_info['electronic_film'],true);

        $arr = [];
        foreach($electronic_film as $val){
            $electronic_film_string = '';
            foreach($val['img'] as $sonVal){
                $electronic_film_string .= $sonVal.",";
            }
            $electronic_film_string = substr($electronic_film_string,0,-1);
            $arr[] = $electronic_film_string;
        }
        // print_r($arr);
        // print_r($electronic_film_string);
        $this->assign("arr",$arr);
        return $this->fetch();

    }

    /**
     *查看分享,生成带有expire的二维码
     */
    public function share_case(){

        $cid = $this->request->param('cid');
        $pid = $this->request->param('pid');
        $isCheck = $this->request->param('isCheck');
        $now_time = date("Y-m-d h:i:s",time());
        //$expire = strtotime(date("Y-m-d h:i:s",strtotime("$now_time +6 minute"))) ;
        $expire = time() ;

        $name = "1111";
        $url = APP_DOMAIN."/index/userfamily/check_case/id/{$cid}/pid/{$pid}/expire/{$expire}/isCheck/{$isCheck}";
        $qrcode_path = $this->generateQrcode($url,$name);

        return APP_DOMAIN.'/'.$qrcode_path;
    }

    public function check_case(){

        $cid = $this->request->param('id');
        $isCheck = $this->request->param('isCheck');
        $expire = $this->request->param('expire');
        $pid = $this->request->param('pid');
        $round_time = time()-$expire;
        $timei=round($round_time/60);
        if($timei>5){
            echo "二维码已过期";
            exit;
        }

        //获取病人信息以及病例
        $this->getPatient_info_case($cid);
        $this->assign("isCheck",$isCheck);
        $this->assign("pid",$pid);
        $this->assign("expire",$expire);
        return $this->fetch();
    }

    public function check_case_test(){
        //http://yixue.youzainet.com/index/userfamily/check_detail/cid/41/pid/65.html
        $cid = 41;
        $isCheck = 1;
        $expire = -1;
        $pid = 65;



        //获取病人信息以及病例
        $this->getPatient_info_case($cid);
        $this->assign("isCheck",$isCheck);
        $this->assign("pid",$pid);
        $this->assign("expire",$expire);
        return $this->fetch('check_case');
    }

	/* 使用方法页面 */
	public function use_method(){
		return $this->fetch();
	}


}
        
        
